import requests
from bs4 import BeautifulSoup
import pymysql
db = pymysql.connect(host='localhost', user='root', password='1234', database='paqu', port=3306, charset='utf8')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS result")
sql = """
        create table result(            
        id int primary key auto_increment,

        name varchar(50),
        chars varchar(50)
  
        );
      """
try:
    cursor.execute(sql)
    print("创建数据库成功")
except Exception as e:
    print("创建数据库失败：case%s" % e)
url =("https://www.ricedata.cn/variety/superice.htm")
headers = {'User-agent': 'Mozilla/5.0'}


response = requests.get(url, headers=headers)
response.encoding='gbk'
r = response.text.encode()
soup = BeautifulSoup(r, "html.parser")
yy=soup.find_all("a")
                                                                                  
for c in yy:
    xm=c.text.strip().strip("\n")
    xp="https://www.ricedata.cn/variety/"+c.get("href")

    sql = """
        insert into result(            
        name,
        chars

        )values("%s","%s")
        """%(xm,xp)
    try:
        cursor.execute(sql)
        db.commit()
    except:
        cursor.rollback()
        print('写入失败')




cursor.close()
db.close()


